unit Con_CodigoFiscal;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Consulta, StdCtrls, Buttons, ExtCtrls, Grids, DBGrids, DbTables, db,
  DBClient, TFlatButtonUnit;

type
  TFrmConCodigoFiscal = class(TFrmConsulta)
    Label1: TLabel;
    edtDescricao: TEdit;
    DbgCodFiscal: TDBGrid;
    cds: TClientDataSet;
    ds: TDataSource;
    cdscodf_codigo: TStringField;
    cdscodf_tipo: TStringField;
    cdscodf_descricao: TStringField;
    btnGravar: TFlatButton;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure edtDescricaoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure BtnFecharClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnGravarClick(Sender: TObject);
  private
    { Private declarations }
    procedure FazConsulta;
  public
    { Public declarations }
    CodF_Codigo: string;
    Mod_Codigo : String;
  end;

var
  FrmConCodigoFiscal: TFrmConCodigoFiscal;

implementation

uses ClsCodFiscal, Dm, Biblio, Cad_CodFiscal, menu;

var
  CodFiscal: TCodFiscal;
  tds: TDataSource;
  tq: TQuery;
{$R *.DFM}

procedure TFrmConCodigoFiscal.FazConsulta;
begin
  if edtDescricao.text = '' then edtDescricao.text := ' ';
  if Mod_Codigo = '' then
    CodFiscal.Consultar(tq, edtDescricao.text)
  else
     CodFiscal.FiltrarModeloCFOP(tq,Mod_Codigo);

  tq.open;
  Cds.close;
  Cds.CreateDataSet;
  Cds.Open;
  while not tq.eof do begin
    cds.append;
    cdscodf_codigo.value := tq.fieldbyname('codf_codigo').asstring;
    cdscodf_tipo.value := tq.fieldbyname('codf_tipo').asstring;
    cdscodf_descricao.value := tq.fieldbyname('codf_descricao').asstring;
    cds.post;
    tq.next;
  end;
  cds.first;
  //DbgCodFiscal.DataSource:= tds;
  DbgCodFiscal.Refresh;
end;

procedure TFrmConCodigoFiscal.FormActivate(Sender: TObject);
begin
  inherited;
  if FileExists(caminho_personal + 'belquimica') then begin
    btnGravar.Visible := False;
  end;
  fazconsulta;
  edtDescricao.setfocus;
end;

procedure TFrmConCodigoFiscal.FormCreate(Sender: TObject);
begin
  inherited;
  CodFiscal := TCodFiscal.Create;
  tds := TDataSource.create(self);
  tq := Tquery.create(self);
  tq.databasename := dm.dmint.db.databasename;
  tq.active := False;
  tds.DataSet := tq;
end;

procedure TFrmConCodigoFiscal.edtDescricaoKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key = 12 then begin
    FazConsulta;
    edtDescricao.setfocus;
  end;
end;

procedure TFrmConCodigoFiscal.BtnFecharClick(Sender: TObject);
begin
  inherited;
  CodF_Codigo := cdscodf_codigo.value; //tq.fieldbyname('CodF_Codigo').asstring;
end;

procedure TFrmConCodigoFiscal.FormDestroy(Sender: TObject);
begin
  inherited;
  CodFiscal.Free;
  tds.free;
  tq.free;
end;

procedure TFrmConCodigoFiscal.btnGravarClick(Sender: TObject);
var
  f1: TFrmCodigoFiscal;
begin
  inherited;
  f1 := TFrmCodigoFiscal.Create(Self);
  f1.btnCadCodFiscal.Visible := False;
  f1.showModal;
  f1.Free;
  FazConsulta;
end;

end.
